/*
 * Created by JFormDesigner on Wed Sep 08 17:36:44 CST 2021
 */

package com.minelx.woods.pc.form;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.minelx.httpclient.core.request.IResponse;
import com.minelx.woods.pc.service.ReviewService;
import com.minelx.woods.pc.service.TaskService;
import com.minelx.woods.pc.service.WoodsSession;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @author aaa
 */
public class ViewReviewsForm extends JFrame {
	public static final int LENGTH = 10;

	private final DefaultListModel<JSONObject> reviewsModel;

	private final ReviewService reviewService;

	private final TaskService taskService;

	private int pageAt;

	public static void main(String[] args) {
		WoodsSession session = new WoodsSession("name", "password");
		new ViewReviewsForm(session.reviewService(), session.taskService()).setVisible(true);
	}

	public ViewReviewsForm(ReviewService reviewService, TaskService taskService) {
		initComponents();
		this.reviewService = reviewService;
		this.taskService = taskService;

		// preparing reviews list cell renderer
		DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
		reviewsList.setCellRenderer((list, value, index, isSelected, cellHasFocus) -> defaultListCellRenderer.getListCellRendererComponent(list,
				value.getString("name"),
				index,
				isSelected,
				cellHasFocus));

		// preparing data model
		reviewsModel = new DefaultListModel<>();
		reviewsList.setModel(reviewsModel);

		// preparing service
		pageAt = 0;

		locate(pageAt);
	}

	private void reviewsListValueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() || reviewsList.getSelectedIndex() == -1) {
			return;
		}
		// show task detail dialog
		JSONObject reviewAsJSON = reviewsModel.getElementAt(reviewsList.getSelectedIndex());
		new ReviewDetailDialog(this, reviewService, taskService, reviewAsJSON).setVisible(true);
	}

	private void prevPageButtonActionPerformed(ActionEvent e) {
		offsetPage(-1);
	}

	private void nextPageButtonActionPerformed(ActionEvent e) {
		offsetPage(1);
	}

	private void offsetPage(int offset) {
		if (pageAt + offset < 0) {
			throw new IndexOutOfBoundsException("page index out of bound: " + (pageAt + offset));
		}
		pageAt += offset;
		locate(pageAt);
	}

	private void locate(int at) {
		IResponse response = reviewService.getReviews(at * LENGTH, LENGTH);

		// restore reviews
		List<JSONObject> reviews = JSON.parseArray(response.contentAsText(UTF_8)).toJavaList(JSONObject.class);
		reviewsModel.clear();
		reviewsModel.addAll(reviews);

		// text
		pageTipsLabel.setText(String.valueOf(at));
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		scrollPane1 = new JScrollPane();
		reviewsList = new JList<>();
		label1 = new JLabel();
		panel1 = new JPanel();
		prevPageButton = new JButton();
		nextPageButton = new JButton();
		pageTipsLabel = new JLabel();

		//======== this ========
		setTitle("\u5ba1\u6838");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout(12, 12));

				//======== scrollPane1 ========
				{

					//---- reviewsList ----
					reviewsList.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					reviewsList.addListSelectionListener(e -> reviewsListValueChanged(e));
					scrollPane1.setViewportView(reviewsList);
				}
				contentPanel.add(scrollPane1, BorderLayout.CENTER);

				//---- label1 ----
				label1.setText("\u5ba1\u6838\u5217\u8868");
				label1.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
				contentPanel.add(label1, BorderLayout.NORTH);

				//======== panel1 ========
				{
					panel1.setLayout(new FlowLayout(FlowLayout.TRAILING, 20, 0));

					//---- prevPageButton ----
					prevPageButton.setText("\u4e0a\u4e00\u9875");
					prevPageButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 20));
					prevPageButton.addActionListener(e -> prevPageButtonActionPerformed(e));
					panel1.add(prevPageButton);

					//---- nextPageButton ----
					nextPageButton.setText("\u4e0b\u4e00\u9875");
					nextPageButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 20));
					nextPageButton.addActionListener(e -> nextPageButtonActionPerformed(e));
					panel1.add(nextPageButton);

					//---- pageTipsLabel ----
					pageTipsLabel.setText("1/1");
					pageTipsLabel.setHorizontalAlignment(SwingConstants.CENTER);
					panel1.add(pageTipsLabel);
				}
				contentPanel.add(panel1, BorderLayout.SOUTH);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		setSize(950, 725);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JScrollPane scrollPane1;
	private JList<JSONObject> reviewsList;
	private JLabel label1;
	private JPanel panel1;
	private JButton prevPageButton;
	private JButton nextPageButton;
	private JLabel pageTipsLabel;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
